// pages/user/address/addressAdd/addressAdd.js
const common = require('../../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        region: [],
        userName: '',
        userPhone: '',
        userAddress: '',
        isDefault: 1
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    nameInput: function(e) {
        this.setData({
            userName: e.detail.value
        })
    },

    phoneInput: function(e) {
        this.setData({
            userPhone: e.detail.value
        })
    },

    addressInput: function(e) {
        this.setData({
            userAddress: e.detail.value
        })
    },

    setDefault: function(e) {
        this.setData({
            isDefault: e.detail.value ? 2 : 1
        })
    },

    bindRegionChange: function(e) {
        this.setData({
            region: e.detail.value
        })
    },

    importAddress: function() {
        const that = this
        wx.chooseAddress({
            success(res) {
                wx.showLoading({
                    title: '正在保存...',
                })
                that.setData({
                    userName: res.userName,
                    userPhone: res.telNumber,
                    userAddress: res.detailInfo,
                    region: [res.provinceName, res.cityName, res.countyName]
                })
                let data = {
                    consignee: that.data.userName,
                    mobile: that.data.userPhone,
                    province: that.data.region[0],
                    city: that.data.region[1],
                    district: that.data.region[2],
                    address: that.data.userAddress,
                    default_id: 2
                }
                common.post('Address/address_add', data).then(function(res) {
                    wx.hideLoading()
                    wx.showToast({
                        title: '新增地址成功',
                        icon: 'none',
                        duration: 1000
                    })
                    wx.navigateBack({
                        delta: 1
                    })
                }).catch(function(e) {})
            },
            fail(err) {
                if (err.errMsg === "chooseAddress:fail auth deny") {
                    wx.showModal({
                        title: '用户未授权',
                        content: '请先开启获取收货地址权限，开启后即导入收货地址',
                        success(res) {
                            if (res.confirm) {
                                wx.openSetting({
                                    success(settingdata) {
                                        if (settingdata.authSetting['scope.address']) {
                                            wx.showToast({
                                                title: '成功开启授权，请重新点击导入地址',
                                                icon: 'none',
                                                duration: 2000
                                            })
                                        } else {
                                            wx.showToast({
                                                title: '开启授权失败，请确认开启授权后重新导入地址',
                                                icon: 'none',
                                                duration: 2000
                                            })
                                        }
                                    }
                                })
                            } else if (res.cancel) {
                                wx.showToast({
                                    title: '开启授权失败，请确认开启授权后重新导入地址',
                                    icon: 'none',
                                    duration: 2000
                                })
                            }
                        }
                    })
                }
            }
        })
    },

    goSaveAddress: function() {
        if (this.data.userName == '') {
            wx.showToast({
                title: '请填写收货人姓名',
                icon: 'none',
                duration: 1000
            })
            return
        }
        if (!(/^1\d{10}$/.test(this.data.userPhone))) {
            wx.showToast({
                title: '手机号有误',
                icon: 'none',
                duration: 1000
            })
            return
        }
        if (this.data.userRegion == '') {
            wx.showToast({
                title: '请填写收货人所在地区',
                icon: 'none',
                duration: 1000
            })
            return
        }
        if (this.data.userAddress == '') {
            wx.showToast({
                title: '请填写收货人详细地址',
                icon: 'none',
                duration: 1000
            })
            return
        }
        let data = {
            consignee: this.data.userName,
            mobile: this.data.userPhone,
            province: this.data.region[0],
            city: this.data.region[1],
            district: this.data.region[2],
            address: this.data.userAddress,
            default_id: this.data.isDefault
        }
        common.post('Address/address_add', data).then(function(res) {
            wx.showToast({
                title: '新增地址成功',
                icon: 'none',
                duration: 1000
            })
            wx.navigateBack({
                delta: 1
            })
        }).catch(function(e) {})
    }
})